package com.wanmait.recycle.dao.impl;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.stereotype.Repository;

import com.wanmait.recycle.dao.SolrDAO;
import com.wanmait.recycle.pojo.Article;
import com.wanmait.recycle.pojo.Firsttype;
import com.wanmait.recycle.pojo.Question;
import com.wanmait.recycle.pojo.Rubbish;
import com.wanmait.recycle.pojo.Secondtype;
import com.wanmait.recycle.pojo.Userinfo;
import com.wanmait.recycle.util.Pager;

@Repository
public class SolrDAOImpl implements SolrDAO {

	@Resource
	private HttpSolrClient client;

	@Override
	public List<Rubbish> findRubbishPage(String keyword, Pager pager) {
		
		client.setBaseURL("http://solr.qinglab.cn/rubbish");
		
		// 定义一个搜素结果
		List<Rubbish> Rubbishs = new ArrayList<>();
		Rubbish Rubbish = null;
		Secondtype secondtype = null;
		Firsttype firsttype = null;
		// 定义solr查询结果
		SolrQuery solrQuery = new SolrQuery();
		// 查询语句
		
		solrQuery.setQuery("name:" + keyword + " OR firsttypeTitle:" + keyword + "  OR content:" + keyword
				+ " OR secondtypeTitle:" + keyword);

		// 设置高亮
		solrQuery.set("hl", "true"); // 默认为false

		solrQuery.set("hl.fl", "name", "firsttypeTitle", "secondtypeTitle");// 设置高亮字段

		solrQuery.set("hl.simple.pre", "<em>"); // 默认为<em>
		solrQuery.set("hl.simple.post", "</em>");// 默认为</em>

		// 设置从第几条开始查询
		solrQuery.setStart(pager.getFirstRow());
		// 设置查询条数
		solrQuery.setRows(pager.getPageSize());
		/*solrQuery.setSort("score", ORDER.asc);*/
		try {

			QueryResponse queryResponse = client.query(solrQuery);
			// 同过查询查询出结果results 这是高亮结果
			SolrDocumentList results = queryResponse.getResults();
			// 设置总条数
			pager.setDataCount((int) results.getNumFound());
			// 获取高亮数据：
			// map的键为主键字段id的值,值map的键为title List<String>为高亮的值，使用List是为了适应有多个值的字段
			Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();

			for (SolrDocument result : results) {

				Rubbish = new Rubbish();
				Rubbish.setId(Integer.parseInt((String) result.get("id")));
				Rubbish.setName((String) result.get("name"));
				Rubbish.setPic((String) result.get("pic"));

				firsttype = new Firsttype();
				firsttype.setId((Integer) result.get("firstTypeId"));
				firsttype.setTitle((String) result.get("firsttypeTitle"));
				firsttype.setIcon((String) result.get("firsttypeIcon"));

				secondtype = new Secondtype();
				secondtype.setId((Integer) result.get("secondtypeId"));
				secondtype.setTitle((String) result.get("secondtypeTitle"));
				secondtype.setIcon((String) result.get("secondtypeIcon"));
				secondtype.setFirsttype(firsttype);
				Rubbish.setSecondtype(secondtype);

				try {
					if (highlighting.get(result.get("id")).get("name") != null) {
						Rubbish.setName(highlighting.get(result.get("id")).get("name").get(0));
						
					}
					if (highlighting.get(result.get("id")).get("secondtypeTitle") != null) {
						secondtype.setTitle(highlighting.get(result.get("id")).get("secondtypeTitle").get(0));
						
						
						Rubbish.setSecondtype(secondtype);
					}
					if (highlighting.get(result.get("id")).get("firsttypeTitle") != null) {

						firsttype.setTitle(highlighting.get(result.get("id")).get("firsttypeTitle").get(0));
						

						secondtype.setFirsttype(firsttype);
						Rubbish.setSecondtype(secondtype);
					}

				} catch (Exception e) {
					Rubbish.setName((String) result.get("name"));
					firsttype.setTitle((String) result.get("firsttypeTitle"));
					secondtype.setTitle((String) result.get("secondtypeTitle"));
					secondtype.setFirsttype(firsttype);
					Rubbish.setSecondtype(secondtype);
				}

				Rubbish.setInputTime((Timestamp) result.get("inputTime"));
				Rubbishs.add(Rubbish);
			}
		} catch (SolrServerException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		return Rubbishs;
	}

	@Override
	public List<Article> findArticlePage(String keyword, Pager pager) {
		// 定义一个搜素结果
		client.setBaseURL("http://solr.qinglab.cn/article");
		
		List<Article> Articles = new ArrayList<>();
		Article article = null;
		Userinfo userinfo = null;
		
		// 定义solr查询结果
		SolrQuery solrQuery = new SolrQuery();
		// 查询语句
		solrQuery.setQuery("nickName:" + keyword + " OR source:" + keyword + "  OR title:" + keyword
				+ " OR content:" + keyword);

		// 设置高亮
		solrQuery.set("hl", "true"); // 默认为false

		solrQuery.set("hl.fl", "nickName", "content", "title","source");// 设置高亮字段

		solrQuery.set("hl.simple.pre", "<em>"); // 默认为<em>
		solrQuery.set("hl.simple.post", "</em>");// 默认为</em>
		

		QueryResponse queryResponse;
		SolrDocumentList results;
		try {
			queryResponse = client.query(solrQuery);
			// 同过查询查询出结果results 这是高亮结果
			results = queryResponse.getResults();
			// 设置总条数
			pager.setDataCount((int) results.getNumFound());
		} catch (SolrServerException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		
		// 设置从第几条开始查询
		solrQuery.setStart(pager.getFirstRow());
		// 设置查询条数
		solrQuery.setRows(pager.getPageSize());
		solrQuery.setSort("score", ORDER.asc);
		try {

			queryResponse = client.query(solrQuery);
			// 同过查询查询出结果results 这是高亮结果
			results = queryResponse.getResults();
			// 设置总条数
			pager.setDataCount((int) results.getNumFound());
			// 获取高亮数据：
			// map的键为主键字段id的值,值map的键为title List<String>为高亮的值，使用List是为了适应有多个值的字段
			Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();

			for (SolrDocument result : results) {
				
				article = new Article();
				article.setId(Integer.parseInt((String) result.get("id")));
				article.setTitle((String) result.get("title"));
				article.setContent((String) result.get("content"));
				article.setSource((String) result.get("source"));
				
				

				userinfo = new Userinfo();
				userinfo.setId((Integer) result.get("userinfoId"));
				userinfo.setNickName((String) result.get("nickName"));
				
				article.setUserinfo(userinfo);

				try {
					if (highlighting.get(result.get("id")).get("title") != null) {
					article.setTitle(highlighting.get(result.get("id")).get("title").get(0));
					}
					if (highlighting.get(result.get("id")).get("content") != null) {
					article.setContent(highlighting.get(result.get("id")).get("content").get(0));
					}
					
					if (highlighting.get(result.get("id")).get("source") != null) {
						article.setSource(highlighting.get(result.get("id")).get("source").get(0));
					}
					if (highlighting.get(result.get("id")).get("nickName") != null) {
						userinfo.setNickName(highlighting.get(result.get("id")).get("nickName").get(0));
					}
					
					

				} catch (Exception e) {
					article.setContent((String) result.get("content"));
					article.setTitle((String) result.get("title"));
					article.setSource((String) result.get("source"));
					userinfo.setNickName((String) result.get("nickName"));
					article.setUserinfo(userinfo);
				}
				article.setPic((String) result.get("pic"));
				
				article.setPraiseCount((Integer) result.get("praiseCount"));
				article.setCommentCount((Integer) result.get("commentCount"));
				DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String dateStr = sdf.format(result.get("inputTime")); 
				article.setInputTime(Timestamp.valueOf(dateStr));
				
				Articles.add(article);
			}
		} catch (SolrServerException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		return Articles;
	}

	@Override
	public List<Question> findQuestionPage(String keyword, Pager pager) {
		// 定义一个搜素结果
		client.setBaseURL("http://solr.qinglab.cn/question");
		
		List<Question> questions = new ArrayList<>();
		Question question = null;
		Userinfo userinfo = null;
		Rubbish rubbish = null; 
		
		// 定义solr查询结果
		SolrQuery solrQuery = new SolrQuery();
		// 查询语句
		solrQuery.setQuery("content:" + keyword + " OR nickName:" + keyword + "  OR name:" + keyword);

		// 设置高亮
		solrQuery.set("hl", "true"); // 默认为false

		solrQuery.set("hl.fl", "nickName", "content", "name");// 设置高亮字段

		solrQuery.set("hl.simple.pre", "<em>"); // 默认为<em>
		solrQuery.set("hl.simple.post", "</em>");// 默认为</em>

		QueryResponse queryResponse;
		SolrDocumentList results;
		try {
			queryResponse = client.query(solrQuery);
			// 同过查询查询出结果results 这是高亮结果
			results = queryResponse.getResults();
			// 设置总条数
			pager.setDataCount((int) results.getNumFound());
		} catch (SolrServerException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		// 设置从第几条开始查询
		solrQuery.setStart(pager.getFirstRow());
		// 设置查询条数
		solrQuery.setRows(pager.getPageSize());
		/*solrQuery.setSort("score", ORDER.asc);*/
		try {

			queryResponse = client.query(solrQuery);
			// 同过查询查询出结果results 这是高亮结果
			results = queryResponse.getResults();
			// 设置总条数
			pager.setDataCount((int) results.getNumFound());
			// 获取高亮数据：
			// map的键为主键字段id的值,值map的键为title List<String>为高亮的值，使用List是为了适应有多个值的字段
			Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();

			for (SolrDocument result : results) {
				
				question = new Question();
				question.setContent((String) result.get("content"));
				question.setId(Integer.parseInt((String) result.get("id")));
				question.setReplyCount((Integer)result.get("replyCount"));
				
				userinfo = new Userinfo();
				userinfo.setId((Integer) result.get("userinfoId"));
				userinfo.setNickName((String) result.get("nickName"));
				
				question.setUserinfo(userinfo);
				
				rubbish = new Rubbish();
				rubbish.setId((Integer) result.get("rubbishId"));
				rubbish.setName((String) result.get("name"));
				question.setRubbish(rubbish);
				try {
					
					if (highlighting.get(result.get("id")).get("content") != null) {
						question.setContent(highlighting.get(result.get("id")).get("content").get(0));
					}
					if (highlighting.get(result.get("id")).get("name") != null) {
						rubbish.setName(highlighting.get(result.get("id")).get("name").get(0));
					}
					if (highlighting.get(result.get("id")).get("nickName") != null) {
						userinfo.setNickName(highlighting.get(result.get("id")).get("nickName").get(0));
					}
					
				} catch (Exception e) {
					question.setContent((String) result.get("content"));
					
					rubbish.setName((String) result.get("name"));
					question.setRubbish(rubbish);
					userinfo.setNickName((String) result.get("nickName"));
					question.setUserinfo(userinfo);
				}
				DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String dateStr = sdf.format(result.get("inputTime")); 
				question.setInputTime(Timestamp.valueOf(dateStr));
				
				questions.add(question);
			}
		} catch (SolrServerException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		return questions;
	}

	

	/*
	 * @Override public List<Rubbish> findPage(String keyword, Pager pager) {
	 * List<Rubbish> Rubbishs = new ArrayList<>(); Rubbish Rubbish = null;
	 * RubbishType RubbishType = null; SolrQuery solrQuery = new SolrQuery();
	 * 
	 * solrQuery.setQuery("title:" + keyword + " OR description:" + keyword +
	 * "  OR info:" + keyword);
	 * 
	 * // 设置高亮 solrQuery.set("hl", "true"); // 默认为false solrQuery.set("hl.fl",
	 * "title");// 设置高亮字段 solrQuery.set("hl.simple.pre",
	 * "<span style=\"color:green\">"); // 默认为<em> solrQuery.set("hl.simple.post",
	 * "</span>");// 默认为</em> //设置从第几条开始查询 solrQuery.setStart(pager.getFirstRow());
	 * //设置查询条数 solrQuery.setRows(pager.getPageSize()); try {
	 * 
	 * QueryResponse queryResponse = client.query(solrQuery);
	 * 
	 * SolrDocumentList results = queryResponse.getResults(); //设置总条数
	 * pager.setDataCount((int)results.getNumFound()); // 获取高亮数据： //
	 * map的键为主键字段id的值,值map的键为title List<String>为高亮的值，使用List是为了适应有多个值的字段 Map<String,
	 * Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
	 * 
	 * for (SolrDocument result : results) {
	 * 
	 * Rubbish = new Rubbish(); Rubbish.setId(Integer.parseInt((String)
	 * result.get("id")));
	 * 
	 * RubbishType = new RubbishType(); RubbishType.setId((Integer)
	 * result.get("RubbishTypeId")); Rubbish.setRubbishType(RubbishType); try {
	 * Rubbish.setTitle(highlighting.get(result.get("id")).get("title").get(0)); }
	 * catch (Exception e) { Rubbish.setTitle((String) result.get("title")); }
	 * 
	 * Rubbish.setInputTime((Date) result.get("inputTime")); Rubbishs.add(Rubbish);
	 * } } catch (SolrServerException e1) { // TODO Auto-generated catch block
	 * e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch
	 * block e1.printStackTrace(); }
	 * 
	 * return Rubbishs; }
	 */

}
